﻿using Caliburn.Micro;
using Microsoft.Win32;
using SD.Common;
using SD.Infrastructure.DTOBase;
using SD.Infrastructure.WPF.Caliburn.Aspects;
using SD.Infrastructure.WPF.Caliburn.Base;
using SD.Infrastructure.WPF.Extensions;
using SD.Infrastructure.WPF.Models;
using SD.IOC.Core.Mediators;
using SD.Toolkits.Excel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.ServiceModel.Extensions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using SlamDunk.MES.IAppService.DTOs.Outputs.ConfigurationContext;
using SlamDunk.MES.IAppService.DTOs.Outputs.ExecutionContext;
using SlamDunk.MES.IAppService.Interfaces;
using SlamDunk.MES.Presentation;
using SlamDunk.MES.Presentation.Enums;
using SlamDunk.MES.Presentation.Maps;
using SlamDunk.MES.Presentation.Models.ExportModels;
using SlamDunk.ValueObjects.Enums;

namespace SlamDunk.MES.Client.ViewModels.Execution
{
    /// <summary>
    /// 工单执行首页ViewModel
    /// </summary>
    public class IndexViewModel : ScreenBase
    {
        #region # 字段及构造器

        /// <summary>
        /// 库存单据服务契约接口代理
        /// </summary>
        private readonly ServiceProxy<IExecutionContract> _iExecutionContract;

        /// <summary>
        /// 窗口管理器
        /// </summary>
        private readonly IWindowManager _windowManager;

        /// <summary>
        /// 依赖注入构造器
        /// </summary>
        public IndexViewModel(ServiceProxy<IExecutionContract> iExecutionContract, IWindowManager windowManager)
        {
            this._iExecutionContract = iExecutionContract;
            this._windowManager = windowManager;

            //默认值
            this.TeamPageIndex = 1;
            this.TeamPageSize = 20;

            this.PersonPageIndex = 1;
            this.PersonPageSize = 20;
        }

        #endregion

        #region # 属性
        #region 班组页码 —— int TeamPageIndex
        /// <summary>
        /// 班组页码
        /// </summary>
        [DependencyProperty]
        public int TeamPageIndex { get; set; }
        #endregion

        #region 班组页容量 —— int TeamPageSize
        /// <summary>
        /// 班组页容量
        /// </summary>
        [DependencyProperty]
        public int TeamPageSize { get; set; }
        #endregion

        #region 班组总记录数 —— int TeamRowCount
        /// <summary>
        /// 班组总记录数
        /// </summary>
        [DependencyProperty]
        public int TeamRowCount { get; set; }
        #endregion

        #region 班组总页数 —— int TeamPageCount
        /// <summary>
        /// 班组总页数
        /// </summary>
        [DependencyProperty]
        public int TeamPageCount { get; set; }
        #endregion

        #region 人员页码 —— int PersonPageIndex
        /// <summary>
        /// 人员页码
        /// </summary>
        [DependencyProperty]
        public int PersonPageIndex { get; set; }
        #endregion

        #region 人员页容量 —— int PersonPageSize
        /// <summary>
        /// 人员页容量
        /// </summary>
        [DependencyProperty]
        public int PersonPageSize { get; set; }
        #endregion

        #region 人员总记录数 —— int PersonRowCount
        /// <summary>
        /// 人员总记录数
        /// </summary>
        [DependencyProperty]
        public int PersonRowCount { get; set; }
        #endregion

        #region 人员总页数 —— int PersonPageCount
        /// <summary>
        /// 人员总页数
        /// </summary>
        [DependencyProperty]
        public int PersonPageCount { get; set; }
        #endregion

        #region 关键字
        /// <summary>
        /// 关键字
        /// </summary>
        [DependencyProperty]
        public string Keywords { get; set; }
        #endregion

        #region 开始时间
        /// <summary>
        /// 开始时间
        /// </summary>
        [DependencyProperty]
        public DateTime? StartTime { get; set; }
        #endregion

        #region 结束时间
        /// <summary>
        /// 结束时间
        /// </summary>
        [DependencyProperty]
        public DateTime? EndTime { get; set; }
        #endregion

        #region 工单状态列表
        /// <summary>
        /// 工单状态列表
        /// </summary>
        [DependencyProperty]
        public IDictionary<string, string> WorkOrderStatusList { get; set; }
        #endregion

        #region 已选工单状态 —— WorkOrderStatus? SelectedWorkOrderStatus
        /// <summary>
        /// 已选工单状态
        /// </summary>
        [DependencyProperty]
        public WorkOrderStatusEnum? SelectedWorkOrderStatus { get; set; }
        #endregion

        #region 班组工单列表
        /// <summary>
        /// 班组工单列表
        /// </summary>
        [DependencyProperty]
        public ObservableCollection<Wrap<WorkOrderInfo>> WorkOrderTeams { get; set; }
        #endregion

        #region 人员工单列表
        /// <summary>
        /// 人员工单列表
        /// </summary>
        [DependencyProperty]
        public ObservableCollection<Wrap<WorkOrderInfo>> WorkOrderPersons { get; set; }
        #endregion

        #endregion

        #region # 方法
        //Initializations
        #region 初始化 —— override async Task OnInitializeAsync(CancellationToken cancellationToken)
        /// <summary>
        /// 初始化
        /// </summary>
        protected override async Task OnInitializeAsync(CancellationToken cancellationToken)
        {
            this.WorkOrderStatusList = typeof(WorkOrderStatusEnum).GetEnumMembers();
            await this.ReloadWorkOrderTeams();
            await this.ReloadWorkOrderPersons();
        }
        #endregion

        //Actions
        #region 全选工单班组 —— void CheckAllTeam()
        /// <summary>
        /// 全选工单班组
        /// </summary>
        public void CheckAllTeam()
        {
            this.WorkOrderTeams.ForEach(x => x.IsChecked = true);
        }
        #endregion

        #region 取消全选工单班组 —— void UncheckAllTeam()
        /// <summary>
        /// 取消全选工单班组
        /// </summary>
        public void UncheckAllTeam()
        {
            this.WorkOrderTeams.ForEach(x => x.IsChecked = false);
        }
        #endregion

        #region 全选工单人员 —— void CheckAllPerson()
        /// <summary>
        /// 全选工单人员
        /// </summary>
        public void CheckAllPerson()
        {
            this.WorkOrderPersons.ForEach(x => x.IsChecked = true);
        }
        #endregion

        #region 取消全选工单人员 —— void UncheckAllPerson()
        /// <summary>
        /// 取消全选工单人员
        /// </summary>
        public void UncheckAllPerson()
        {
            this.WorkOrderPersons.ForEach(x => x.IsChecked = false);
        }
        #endregion

        #region 加载工单列表 —— async void LoadWorkOrders()
        /// <summary>
        /// 加载工单列表
        /// </summary>
        public async void LoadWorkOrders()
        {
            await this.ReloadWorkOrderTeams();
            await this.ReloadWorkOrderPersons();
        }
        #endregion

        #region 工单查看 —— async void WorkOrderActualParameter()
        /// <summary>
        /// 工单查看
        /// </summary>
        /// <param name=""></param>
        public async void LookWorkOrde()
        {
            #region # 验证
            WorkOrderInfo[] checkedRoleTeams = this.WorkOrderTeams.Where(x => x.IsChecked == true).Select(x => x.Model).ToArray();
            WorkOrderInfo[] checkedRolePersons = this.WorkOrderPersons.Where(x => x.IsChecked == true).Select(x => x.Model).ToArray();
            if (!checkedRoleTeams.Any() && !checkedRolePersons.Any())
            {
                MessageBox.Show("请勾选一条工单信息！", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
            int checkedRoleCount = checkedRoleTeams.Count() + checkedRolePersons.Count();
            if (checkedRoleCount < 1)
            {
                MessageBox.Show("请勾选一条工单信息！", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
            #endregion
            if (checkedRoleTeams.Any())
            {
                LookViewModel viewModel = ResolveMediator.Resolve<LookViewModel>();
                viewModel.Load(checkedRoleTeams.FirstOrDefault());
                bool? result = await this._windowManager.ShowDialogAsync(viewModel);
                if (result == true)
                {
                    await this.ReloadWorkOrderTeams();
                    await this.ReloadWorkOrderPersons();
                }
            }
            if (checkedRolePersons.Any())
            {
                LookViewModel viewModel = ResolveMediator.Resolve<LookViewModel>();
                viewModel.Load(checkedRolePersons.FirstOrDefault());
                bool? result = await this._windowManager.ShowDialogAsync(viewModel);
                if (result == true)
                {
                    await this.ReloadWorkOrderTeams();
                    await this.ReloadWorkOrderPersons();
                }
            }
        }
        #endregion

        #region 执行工单 —— async void ExecuteWorkOrder()
        /// <summary>
        /// 执行工单
        /// </summary>
        /// <param name=""></param>
        public async void ExecuteWorkOrder()
        {
            #region # 验证
            WorkOrderInfo[] checkedRoleTeams = this.WorkOrderTeams.Where(x => x.IsChecked == true).Select(x => x.Model).ToArray();
            WorkOrderInfo[] checkedRolePersons = this.WorkOrderPersons.Where(x => x.IsChecked == true).Select(x => x.Model).ToArray();
            if (!checkedRoleTeams.Any() && !checkedRolePersons.Any())
            {
                MessageBox.Show("请最少勾选一条工单信息！", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
            foreach (var item in checkedRoleTeams)
            {
                if (item.Status == WorkOrderStatus.Completed)
                {
                    MessageBox.Show("当前勾选班组工单信息中存在状态已完成工单！", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }
            }
            foreach (var item in checkedRolePersons)
            {
                if (item.Status == WorkOrderStatus.Completed)
                {
                    MessageBox.Show("当前勾选人员工单信息中存在状态已完成工单！", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }
            }
            #endregion
            MessageBoxResult result = MessageBox.Show("确定要执行当前选中工单吗？", "提示", MessageBoxButton.YesNo, MessageBoxImage.Warning);
            if (result == MessageBoxResult.Yes)
            {
                foreach (var item in checkedRoleTeams)
                {
                    await Task.Run(() => this._iExecutionContract.Channel.ExecuteWorkOrder(item.Id));
                }
                foreach (var item in checkedRolePersons)
                {
                    await Task.Run(() => this._iExecutionContract.Channel.ExecuteWorkOrder(item.Id));
                }
            }
            await this.ReloadWorkOrderTeams();
            await this.ReloadWorkOrderPersons();
        }
        #endregion

        #region 中止工单 —— async void AbortWorkOrder()
        /// <summary>
        /// 中止工单
        /// </summary>
        /// <param name=""></param>
        public async void AbortWorkOrder()
        {
            #region # 验证
            WorkOrderInfo[] checkedRoleTeams = this.WorkOrderTeams.Where(x => x.IsChecked == true).Select(x => x.Model).ToArray();
            WorkOrderInfo[] checkedRolePersons = this.WorkOrderPersons.Where(x => x.IsChecked == true).Select(x => x.Model).ToArray();
            if (!checkedRoleTeams.Any() && !checkedRolePersons.Any())
            {
                MessageBox.Show("请勾选一条工单信息！", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
            int checkedRoleCount = checkedRoleTeams.Count() + checkedRolePersons.Count();
            if (checkedRoleCount < 1)
            {
                MessageBox.Show("请勾选一条工单信息！", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
            #endregion
            if (checkedRoleTeams.Any())
            {
                AbortViewModel viewModel = ResolveMediator.Resolve<AbortViewModel>();
                viewModel.Load(checkedRoleTeams.FirstOrDefault());
                bool? result = await this._windowManager.ShowDialogAsync(viewModel);
                if (result == true)
                {
                    await this.ReloadWorkOrderTeams();
                    await this.ReloadWorkOrderPersons();
                }
            }
            if (checkedRolePersons.Any())
            {
                AbortViewModel viewModel = ResolveMediator.Resolve<AbortViewModel>();
                viewModel.Load(checkedRolePersons.FirstOrDefault());
                bool? result = await this._windowManager.ShowDialogAsync(viewModel);
                if (result == true)
                {
                    await this.ReloadWorkOrderTeams();
                    await this.ReloadWorkOrderPersons();
                }
            }
        }
        #endregion

        #region 完成工单 —— async void CompleteWorkOrder()
        /// <summary>
        /// 完成工单
        /// </summary>
        /// <param name=""></param>
        public async void CompleteWorkOrder()
        {
            #region # 验证
            WorkOrderInfo[] checkedRoleTeams = this.WorkOrderTeams.Where(x => x.IsChecked == true).Select(x => x.Model).ToArray();
            WorkOrderInfo[] checkedRolePersons = this.WorkOrderPersons.Where(x => x.IsChecked == true).Select(x => x.Model).ToArray();
            if (!checkedRoleTeams.Any() && !checkedRolePersons.Any())
            {
                MessageBox.Show("请最少勾选一条工单信息！", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
            foreach (var item in checkedRoleTeams)
            {
                if (item.Status == WorkOrderStatus.Aborted)
                {
                    MessageBox.Show("当前勾选班组工单信息中存在状态已中止工单！", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }
            }
            foreach (var item in checkedRolePersons)
            {
                if (item.Status == WorkOrderStatus.Aborted)
                {
                    MessageBox.Show("当前勾选人员工单信息中存在状态已中止工单！", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }
            }
            #endregion
            MessageBoxResult result = MessageBox.Show("确定要完成当前选中工单吗？", "提示", MessageBoxButton.YesNo, MessageBoxImage.Warning);
            if (result == MessageBoxResult.Yes)
            {
                foreach (var item in checkedRoleTeams)
                {
                    await Task.Run(() => this._iExecutionContract.Channel.CompleteWorkOrder(item.Id));
                }
                foreach (var item in checkedRolePersons)
                {
                    await Task.Run(() => this._iExecutionContract.Channel.CompleteWorkOrder(item.Id));
                }
            }
            await this.ReloadWorkOrderTeams();
            await this.ReloadWorkOrderPersons();
        }
        #endregion

        #region 工单人员工时 —— async void WorkOrderActualPerson()
        /// <summary>
        /// 工单人员工时
        /// </summary>
        /// <param name=""></param>
        public async void WorkOrderActualPerson()
        {
            #region # 验证
            WorkOrderInfo[] checkedRoleTeams = this.WorkOrderTeams.Where(x => x.IsChecked == true).Select(x => x.Model).ToArray();
            WorkOrderInfo[] checkedRolePersons = this.WorkOrderPersons.Where(x => x.IsChecked == true).Select(x => x.Model).ToArray();
            if (!checkedRoleTeams.Any() && !checkedRolePersons.Any())
            {
                MessageBox.Show("请勾选一条工单信息！", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
            int checkedRoleCount = checkedRoleTeams.Count() + checkedRolePersons.Count();
            if (checkedRoleCount < 1)
            {
                MessageBox.Show("请勾选一条工单信息！", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
            #endregion
            if (checkedRoleTeams.Any())
            {
                if (checkedRoleTeams.FirstOrDefault().Status == WorkOrderStatus.Completed)
                {
                    MessageBox.Show("勾选工单信息已完成，无法进行操作！", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }
                ActualPersonsViewModel viewModel = ResolveMediator.Resolve<ActualPersonsViewModel>();
                viewModel.Load(checkedRoleTeams.FirstOrDefault());
                bool? result = await this._windowManager.ShowDialogAsync(viewModel);
                if (result == true)
                {
                    await this.ReloadWorkOrderTeams();
                    await this.ReloadWorkOrderPersons();
                }
            }
            if (checkedRolePersons.Any())
            {
                if (checkedRolePersons.FirstOrDefault().Status == WorkOrderStatus.Completed)
                {
                    MessageBox.Show("勾选工单信息已完成，无法进行操作！", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }
                ActualPersonsViewModel viewModel = ResolveMediator.Resolve<ActualPersonsViewModel>();
                viewModel.Load(checkedRolePersons.FirstOrDefault());
                bool? result = await this._windowManager.ShowDialogAsync(viewModel);
                if (result == true)
                {
                    await this.ReloadWorkOrderTeams();
                    await this.ReloadWorkOrderPersons();
                }
            }
        }
        #endregion

        #region 工单设备使用 —— async void WorkOrderActualEquipment()
        /// <summary>
        /// 工单设备使用
        /// </summary>
        /// <param name=""></param>
        public async void WorkOrderActualEquipment()
        {
            #region # 验证
            WorkOrderInfo[] checkedRoleTeams = this.WorkOrderTeams.Where(x => x.IsChecked == true).Select(x => x.Model).ToArray();
            WorkOrderInfo[] checkedRolePersons = this.WorkOrderPersons.Where(x => x.IsChecked == true).Select(x => x.Model).ToArray();
            if (!checkedRoleTeams.Any() && !checkedRolePersons.Any())
            {
                MessageBox.Show("请勾选一条工单信息！", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
            int checkedRoleCount = checkedRoleTeams.Count() + checkedRolePersons.Count();
            if (checkedRoleCount < 1)
            {
                MessageBox.Show("请勾选一条工单信息！", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
            #endregion
            if (checkedRoleTeams.Any())
            {
                if (checkedRoleTeams.FirstOrDefault().Status == WorkOrderStatus.Completed)
                {
                    MessageBox.Show("勾选工单信息已完成，无法进行操作！", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }
                ActualEquipmentsViewModel viewModel = ResolveMediator.Resolve<ActualEquipmentsViewModel>();
                viewModel.Load(checkedRoleTeams.FirstOrDefault());
                bool? result = await this._windowManager.ShowDialogAsync(viewModel);
                if (result == true)
                {
                    await this.ReloadWorkOrderTeams();
                    await this.ReloadWorkOrderPersons();
                }
            }
            if (checkedRolePersons.Any())
            {
                if (checkedRolePersons.FirstOrDefault().Status == WorkOrderStatus.Completed)
                {
                    MessageBox.Show("勾选工单信息已完成，无法进行操作！", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }
                ActualEquipmentsViewModel viewModel = ResolveMediator.Resolve<ActualEquipmentsViewModel>();
                viewModel.Load(checkedRolePersons.FirstOrDefault());
                bool? result = await this._windowManager.ShowDialogAsync(viewModel);
                if (result == true)
                {
                    await this.ReloadWorkOrderTeams();
                    await this.ReloadWorkOrderPersons();
                }
            }
        }
        #endregion

        #region 工单物料投入 —— async void WorkOrderActualInputMaterial()
        /// <summary>
        /// 工单物料投入
        /// </summary>
        /// <param name=""></param>
        public async void WorkOrderActualInputMaterial()
        {
            #region # 验证
            WorkOrderInfo[] checkedRoleTeams = this.WorkOrderTeams.Where(x => x.IsChecked == true).Select(x => x.Model).ToArray();
            WorkOrderInfo[] checkedRolePersons = this.WorkOrderPersons.Where(x => x.IsChecked == true).Select(x => x.Model).ToArray();
            if (!checkedRoleTeams.Any() && !checkedRolePersons.Any())
            {
                MessageBox.Show("请勾选一条工单信息！", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
            int checkedRoleCount = checkedRoleTeams.Count() + checkedRolePersons.Count();
            if (checkedRoleCount < 1)
            {
                MessageBox.Show("请勾选一条工单信息！", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
            #endregion
            if (checkedRoleTeams.Any())
            {
                if (checkedRoleTeams.FirstOrDefault().Status == WorkOrderStatus.Completed)
                {
                    MessageBox.Show("勾选工单信息已完成，无法进行操作！", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }
                ActualInputMaterialsViewModel viewModel = ResolveMediator.Resolve<ActualInputMaterialsViewModel>();
                viewModel.Load(checkedRoleTeams.FirstOrDefault());
                bool? result = await this._windowManager.ShowDialogAsync(viewModel);
                if (result == true)
                {
                    await this.ReloadWorkOrderTeams();
                    await this.ReloadWorkOrderPersons();
                }
            }
            if (checkedRolePersons.Any())
            {
                if (checkedRolePersons.FirstOrDefault().Status == WorkOrderStatus.Completed)
                {
                    MessageBox.Show("勾选工单信息已完成，无法进行操作！", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }
                ActualInputMaterialsViewModel viewModel = ResolveMediator.Resolve<ActualInputMaterialsViewModel>();
                viewModel.Load(checkedRolePersons.FirstOrDefault());
                bool? result = await this._windowManager.ShowDialogAsync(viewModel);
                if (result == true)
                {
                    await this.ReloadWorkOrderTeams();
                    await this.ReloadWorkOrderPersons();
                }
            }
        }
        #endregion

        #region 工单物料产出 —— async void WorkOrderActualOutputMaterial()
        /// <summary>
        /// 工单物料产出
        /// </summary>
        /// <param name=""></param>
        public async void WorkOrderActualOutputMaterial()
        {
            #region # 验证
            WorkOrderInfo[] checkedRoleTeams = this.WorkOrderTeams.Where(x => x.IsChecked == true).Select(x => x.Model).ToArray();
            WorkOrderInfo[] checkedRolePersons = this.WorkOrderPersons.Where(x => x.IsChecked == true).Select(x => x.Model).ToArray();
            if (!checkedRoleTeams.Any() && !checkedRolePersons.Any())
            {
                MessageBox.Show("请勾选一条工单信息！", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
            int checkedRoleCount = checkedRoleTeams.Count() + checkedRolePersons.Count();
            if (checkedRoleCount < 1)
            {
                MessageBox.Show("请勾选一条工单信息！", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
            #endregion
            if (checkedRoleTeams.Any())
            {
                if (checkedRoleTeams.FirstOrDefault().Status == WorkOrderStatus.Completed)
                {
                    MessageBox.Show("勾选工单信息已完成，无法进行操作！", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }
                ActualOutputMaterialsViewModel viewModel = ResolveMediator.Resolve<ActualOutputMaterialsViewModel>();
                viewModel.Load(checkedRoleTeams.FirstOrDefault());
                bool? result = await this._windowManager.ShowDialogAsync(viewModel);
                if (result == true)
                {
                    await this.ReloadWorkOrderTeams();
                    await this.ReloadWorkOrderPersons();
                }
            }
            if (checkedRolePersons.Any())
            {
                if (checkedRolePersons.FirstOrDefault().Status == WorkOrderStatus.Completed)
                {
                    MessageBox.Show("勾选工单信息已完成，无法进行操作！", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }
                ActualOutputMaterialsViewModel viewModel = ResolveMediator.Resolve<ActualOutputMaterialsViewModel>();
                viewModel.Load(checkedRolePersons.FirstOrDefault());
                bool? result = await this._windowManager.ShowDialogAsync(viewModel);
                if (result == true)
                {
                    await this.ReloadWorkOrderTeams();
                    await this.ReloadWorkOrderPersons();
                }
            }
        }
        #endregion

        #region 工单实际参数 —— async void WorkOrderActualParameter()
        /// <summary>
        /// 工单实际参数
        /// </summary>
        /// <param name=""></param>
        public async void WorkOrderActualParameter()
        {
            #region # 验证
            WorkOrderInfo[] checkedRoleTeams = this.WorkOrderTeams.Where(x => x.IsChecked == true).Select(x => x.Model).ToArray();
            WorkOrderInfo[] checkedRolePersons = this.WorkOrderPersons.Where(x => x.IsChecked == true).Select(x => x.Model).ToArray();
            if (!checkedRoleTeams.Any() && !checkedRolePersons.Any())
            {
                MessageBox.Show("请勾选一条工单信息！", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
            int checkedRoleCount = checkedRoleTeams.Count() + checkedRolePersons.Count();
            if (checkedRoleCount < 1)
            {
                MessageBox.Show("请勾选一条工单信息！", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
            #endregion
            if (checkedRoleTeams.Any())
            {
                if (checkedRoleTeams.FirstOrDefault().Status == WorkOrderStatus.Completed)
                {
                    MessageBox.Show("勾选工单信息已完成，无法进行操作！", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }
                ActualParametersViewModel viewModel = ResolveMediator.Resolve<ActualParametersViewModel>();
                viewModel.Load(checkedRoleTeams.FirstOrDefault());
                bool? result = await this._windowManager.ShowDialogAsync(viewModel);
                if (result == true)
                {
                    await this.ReloadWorkOrderTeams();
                    await this.ReloadWorkOrderPersons();
                }
            }
            if (checkedRolePersons.Any())
            {
                if (checkedRolePersons.FirstOrDefault().Status == WorkOrderStatus.Completed)
                {
                    MessageBox.Show("勾选工单信息已完成，无法进行操作！", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }
                ActualParametersViewModel viewModel = ResolveMediator.Resolve<ActualParametersViewModel>();
                viewModel.Load(checkedRolePersons.FirstOrDefault());
                bool? result = await this._windowManager.ShowDialogAsync(viewModel);
                if (result == true)
                {
                    await this.ReloadWorkOrderTeams();
                    await this.ReloadWorkOrderPersons();
                }
            }
        }
        #endregion

        #region 导出
        /// <summary>
        /// 导出
        /// </summary>
        public async void Export()
        {
            //班组
            List<WorkOrderStatus> statuses = new List<WorkOrderStatus>();
            if (SelectedWorkOrderStatus != null)
            {
                statuses.Add((WorkOrderStatus)SelectedWorkOrderStatus.Value);
            }
            else
            {
                statuses.Add(WorkOrderStatus.Dispatched);
                statuses.Add(WorkOrderStatus.Executing);
            }
            UserExtensionInfo userExtensionInfo = (UserExtensionInfo)AppDomain.CurrentDomain.GetData(Constants.UserExtensionKey);
            PageModel<WorkOrderInfo> pageTeamModel = await Task.Run(() => this._iExecutionContract.Channel.GetWorkOrdersByPage(
                Keywords,//关键字
                null,//生产计划Id
                null, //作业计划Id
                null, //工艺段Id
                null, //工位Id
                null, //是否需质检
                null,//是否已质检
                userExtensionInfo?.TeamInfo?.Id,//分派班组Id
                null,//分派人员Id
                StartTime, //开始时间
                EndTime, //结束时间
                null, //工厂模型Id
                statuses, //工单状态集
                1,//页码
                int.MaxValue));
            //人员
            PageModel<WorkOrderInfo> pagePersonModel = await Task.Run(() => this._iExecutionContract.Channel.GetWorkOrdersByPage(
               Keywords,//关键字
               null,//生产计划Id
               null, //作业计划Id
               null, //工艺段Id
               null, //工位Id
               null, //是否需质检
               null,//是否已质检
               null,//分派班组Id
               userExtensionInfo?.PersonId,//分派人员Id
               StartTime, //开始时间
               EndTime, //结束时间
               null, //工厂模型Id
               statuses, //工单状态集
                1,//页码
                int.MaxValue));

            IList<WorkOrderEm> workOrderEms = new List<WorkOrderEm>();
            workOrderEms.AddRange(pageTeamModel.Datas.Select(x => x.ToExportView()));
            workOrderEms.AddRange(pagePersonModel.Datas.Select(x => x.ToExportView()));
            if (!workOrderEms.Any())
            {
                MessageBox.Show("导出数据为空，不能进行导出！", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            SaveFileDialog saveFileDialog = new SaveFileDialog
            {
                Filter = Constants.ExeclFileExtensionsFilter,
                FileName = $"工单_{DateTime.Now.ToString(Constants.ExportFileTimeFormat)}",
                AddExtension = true,
                RestoreDirectory = true
            };
            if (saveFileDialog.ShowDialog() == true)
            {
                string filePath = saveFileDialog.FileName;
                ExcelWriter.WriteFile(workOrderEms, filePath);
            }
        }
        #endregion
        //Private
        #region 加载工单班组列表 —— async Task ReloadWorkOrderTeams()
        /// <summary>
        /// 加载工单班组列表
        /// </summary>
        private async Task ReloadWorkOrderTeams()
        {
            this.Busy();
            List<WorkOrderStatus> statuses = new List<WorkOrderStatus>();
            if (SelectedWorkOrderStatus != null)
            {
                statuses.Add((WorkOrderStatus)SelectedWorkOrderStatus.Value);
            }
            else
            {
                statuses.Add(WorkOrderStatus.Dispatched);
                statuses.Add(WorkOrderStatus.Executing);
            }
            UserExtensionInfo userExtensionInfo = (UserExtensionInfo)AppDomain.CurrentDomain.GetData(Constants.UserExtensionKey);
            PageModel<WorkOrderInfo> pageModel = await Task.Run(() => this._iExecutionContract.Channel.GetWorkOrdersByPage(
                Keywords,//关键字
                null,//生产计划Id
                null, //作业计划Id
                null, //工艺段Id
                null, //工位Id
                null, //是否需质检
                null,//是否已质检
                userExtensionInfo?.TeamInfo?.Id,//分派班组Id
                null,//分派人员Id
                StartTime, //开始时间
                EndTime, //结束时间
                null, //工厂模型Id
                statuses, //工单状态集
                this.TeamPageIndex,//页码
                this.TeamPageSize));
            this.TeamRowCount = pageModel.RowCount;
            this.TeamPageCount = pageModel.PageCount;

            IEnumerable<Wrap<WorkOrderInfo>> wrapModels = pageModel.Datas.Select(x => x.Wrap());
            this.WorkOrderTeams = new ObservableCollection<Wrap<WorkOrderInfo>>(wrapModels);

            this.Idle();
        }
        #endregion

        #region 加载工单人员列表 —— async Task ReloadWorkOrderPersons()
        /// <summary>
        /// 加载工单人员列表
        /// </summary>
        private async Task ReloadWorkOrderPersons()
        {
            this.Busy();
            List<WorkOrderStatus> statuses = new List<WorkOrderStatus>();
            if (SelectedWorkOrderStatus != null)
            {
                statuses.Add((WorkOrderStatus)SelectedWorkOrderStatus.Value);
            }
            else
            {
                statuses.Add(WorkOrderStatus.Dispatched);
                statuses.Add(WorkOrderStatus.Executing);
            }
            UserExtensionInfo userExtensionInfo = (UserExtensionInfo)AppDomain.CurrentDomain.GetData(Constants.UserExtensionKey);
            PageModel<WorkOrderInfo> pageModel = await Task.Run(() => this._iExecutionContract.Channel.GetWorkOrdersByPage(
                Keywords,//关键字
                null,//生产计划Id
                null, //作业计划Id
                null, //工艺段Id
                null, //工位Id
                null, //是否需质检
                null,//是否已质检
                null,//分派班组Id
                userExtensionInfo?.PersonId,//分派人员Id
                StartTime, //开始时间
                EndTime, //结束时间
                null, //工厂模型Id
                statuses, //工单状态集
                this.TeamPageIndex,//页码
                this.TeamPageSize));
            this.PersonRowCount = pageModel.RowCount;
            this.PersonPageCount = pageModel.PageCount;

            IEnumerable<Wrap<WorkOrderInfo>> wrapModels = pageModel.Datas.Select(x => x.Wrap());
            this.WorkOrderPersons = new ObservableCollection<Wrap<WorkOrderInfo>>(wrapModels);

            this.Idle();
        }
        #endregion
        #endregion
    }
}